//package com.yxc.elasticsearchdemo.task;
//
//import com.alibaba.fastjson.JSON;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.io.FileUtils;
//import org.apache.commons.io.LineIterator;
//import org.elasticsearch.action.bulk.BulkRequest;
//import org.elasticsearch.action.bulk.BulkResponse;
//import org.elasticsearch.action.index.IndexRequest;
//import org.elasticsearch.client.RequestOptions;
//import org.elasticsearch.client.RestHighLevelClient;
//import org.elasticsearch.xcontent.XContentType;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.CommandLineRunner;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.core.annotation.Order;
//import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
//import org.springframework.stereotype.Component;
//
//import java.io.File;
//import java.io.IOException;
//
///**
// * @author: 杨旭晨
// * @time: 2022/6/24 16:23
// **/
//@Slf4j
//@Component
//@Configuration
//@Order(value = 2)
//public class InsertTask implements CommandLineRunner {
//
//    @Autowired
//    RestHighLevelClient client;
//    @Autowired
//    ThreadPoolTaskExecutor taskExecutor;
//
//    @Override
//    public void run(String... args) throws Exception {
//        String filePath = "/data/app/hunting/data/part-3-64";
////        String filePath = "D:\\data\\sl\\part-3-64";
//        File file = new File(filePath);
//        long startTime = System.currentTimeMillis();
//        for (int i = 0; i < 30; i++) {
//            taskExecutor.execute(() -> {
//                LineIterator lineIterator = null;
//                try {
//                    lineIterator = FileUtils.lineIterator(file);
//
//                    BulkRequest bulkRequest = new BulkRequest();
//                    while (lineIterator.hasNext()) {
//                        String logLine = lineIterator.next();
//                        bulkRequest.add(new IndexRequest("big_data1").source(JSON.parseObject(logLine), XContentType.JSON));
//                        if (bulkRequest.numberOfActions() >= 10000) {
//                            // 10000条数据插入一次
//                            try {
//                                BulkResponse bulk = client.bulk(bulkRequest, RequestOptions.DEFAULT);
//                                System.out.println(bulk.status());
//                            } catch (IOException e) {
//                                e.printStackTrace();
//                            }
//                            bulkRequest = new BulkRequest();
//                        }
//                    }
//                    if (bulkRequest.numberOfActions() > 0) {
//                        // 插入剩下数据
//                        try {
//                            client.bulk(bulkRequest, RequestOptions.DEFAULT);
//                        } catch (IOException e) {
//                            e.printStackTrace();
//                        }
//                    }
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            });
//        }
//        Thread.sleep(1000);
//        while (true) {
//            if (taskExecutor.getActiveCount() <= 0) {
//                break;
//            }
//            Thread.sleep(1000);
//        }
//        long endTime = System.currentTimeMillis();
//        log.info("执行完毕，用时{}", endTime - startTime);
//    }
//}
